<?php 
namespace Admin\Model;
use Think\Model;
class ScoreModel extends Model{
	public function getAll($username){

        $levelArray=array('注册会员','铜牌会员','银牌会员','金牌会员','钻石会员');
        $score_info = M("Orders")->where("status=0")->group("u_id")->field('u_id,total')->select();
        foreach($score_info as $key=>$value){
            $data['grades']=round($value['total']/2);
            if($data['grades']==0){
                $data['level']=0;
            }elseif($data['grades']>0 && $data['grades']<=2000){
                $data['level']=1;
            }elseif($data['grades']>2000 && $data['grades']<=4000){
                $data['level']=2;
            }elseif($data['grades']>4000 && $data['grades']<=6000){
                $data['level']=3;
            }else{
                $data['level']=4;
            }
            M('score')->where("u_id= '".$value['u_id']."'")->save($data);
        }
        if ($username) {
            $where['username']=array('like', '%'.$username.'%');
            $uid_arr= D('User')->where($where)->getField('id',true);
            $uid_str=implode(',',$uid_arr);
            if(!empty($uid_str)){
                $list = $this->where("u_id in ($uid_str)")->select();
            }else{
                $list='';
            }
        }else{
            $list = $this->select();
        }
        foreach($list as $key=>$value){
            $list[$key]['username']=M('User')->where("id ='".$value['u_id']."' ")->getField('username');
            $list[$key]['level']=$levelArray[$value['level']];
        }
		return $list;
	}
}


 ?>
